package com.javaPro.myProject.modules.login.controller;


import com.javaPro.myProject.common.co.Const;
import com.javaPro.myProject.common.model.R;

import com.javaPro.myProject.modules.user.model.User;
import com.javaPro.myProject.modules.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    UserService userService;


    @GetMapping({"/login"})// 跳转到登陆界面
    public String login() {
        return "login";
    }

    /**
     * 登录接口
     * @param session
     * @param account 前端传过来的账号
     * @param password 前端传过来的密码
     * @return
     */
    @PostMapping({"/toLogin"})//映射一个POST请求,能够唯一标识一个方法体
    @ResponseBody//必要的，将java对象转为json格式的数据，从而展示在前端上
    public R toLogin(HttpSession session, String account, String password) {
        log.info("你当前登录的用户是：{}，密码是：{}", account, password);


        User user = new User();//实例化一个User对象
        user.setAccount(account);
        User user1= userService.getUser(user);//admin的信息

        if (user1 == null) {
            log.info("登录用户不存在");
            return R.fail("登录用户不存在");
        } else if (user1.getAccount() != null && !user1.getPassword().equalsIgnoreCase(password)) {
            log.info("密码错误");
            return R.fail("密码错误");
        } else if (user1.getAccount() != null && user1.getPassword().equalsIgnoreCase(password)) {
//            String session_user = user1.getAccount();
            if ("1".equals(user1.getStatus()) ){
                session.setAttribute("session_user", user1);
                log.info("密码正确");
                Map<String,String> map = new HashMap<>();
                map.put("userId",user1.getId());
                map.put("role",user1.getRole());
                return R.success(map);
            }else  if ("0".equals(user1.getStatus()) ){
                    return R.fail("账号已被停用");
            }else {
                return R.fail("账号暂未通过审核");
            }

        } else {
            return R.fail("密码和确认密码不一致");
        }
    }
    @PostMapping({"/toRegister"})//映射一个POST请求,能够唯一标识一个方法体
    @ResponseBody//必要的，将java对象转为json格式的数据，从而展示在前端上
    public R toRegister( User param) {
boolean flag = false;//true  false
        if (param.getLisenCode() != null && !"".equals(param.getLisenCode())){
            if (Const.lisenCode.equals(param.getLisenCode())){
                flag = true;
            }
        }else {
            if (param.getRole().equals("2")){
                //需要授权码
            }else {
                flag = true;
            }

        }
if (flag){
    User user = new User();//实例化一个User对象
    user.setAccount(param.getAccount());
    user.setPassword(param.getPassword());
    user.setRole(param.getRole());
    user.setUserName(param.getUserName());
    user.setStatus("1");

    User user1= userService.getUser(user);

    if (user1 != null) {
        return R.fail("用户已经存在");
    }  else {
        if ("".equals(param.getPassword()) ){
            return R.fail("密码为空！");
        }
        int i = userService.insertUserAcc(user);
        if ( i > 0){
            User justRegiUser = userService.getUser(param);
            justRegiUser.setUserId(justRegiUser.getId());
            if ("1".equals(param.getRole())){//
                userService.insertUser(justRegiUser);
            }
        }

        return R.success("注册成功！");
    }
}else {
    return R.fail("抱歉，请输入正确的授权码!");
}

    }
    @GetMapping({"/admin/logout"})
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login";
    }
}
